package com.yd.app.mapper.provider;

import com.yd.domain.business.GType;
import com.yd.domain.searchParam.GTypeParam;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zy
 * @create 2018-03-08 17:14
 * @desc 商品类型
 **/
public class GTypeProvider extends BaseProvider{
    /**
     * @Author: zy
     * @Desc: 分页查询数据
     * @Date:  2018/3/9
     * @Param
     */
    public String searchGTypeList(Map map){
        GTypeParam gTypeParam= (GTypeParam) map.get("gTypeParam");
        StringBuilder sb=new StringBuilder("select o.id,o.typeName,o.price,o.ifUse,o.goodKind  FROM business_gType o " +
                " WHERE 1=1 ");
        sb.append(createSearchSanWhereSql(gTypeParam));
        sb.append("  limit #{startRow},#{pageSize}");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询数据条数
     * @Date:  2018/3/7
     */
    public String searchGTypeCount(Map map){
        GTypeParam gTypeParam= (GTypeParam) map.get("gTypeParam");
        StringBuilder sb=new StringBuilder("SELECT COUNT(1) from business_gType o  " +
                "  where 1=1 ");
        sb.append(createSearchSanWhereSql(gTypeParam));
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc:查询条件
     * @Date:  2018/3/7
     */
    private String createSearchSanWhereSql(GTypeParam gTypeParam){
        StringBuilder sb=new StringBuilder();
        if(!StringUtils.isEmpty(gTypeParam.getTypeName())){
            sb.append(" and o.typeName like  '%"+gTypeParam.getTypeName()+"%'  ");
        }
        if(!StringUtils.isEmpty(gTypeParam.getGoodKind())){
            sb.append(" and o.goodKind = '"+gTypeParam.getGoodKind()+"'  ");
        }
       if(gTypeParam.isIfUse()){
            sb.append(" and o.ifUse = "+true+"  ");
        }
        if(!gTypeParam.isIfUse()){
            sb.append(" and o.ifUse = "+false+"  ");
        }
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc: 批量修改启用或者是停用
     * @Date:  2018/3/9
     * @Param
     */
    public String updateAllIfUse(Map map){
        GType gType=(GType)map.get("gType");
        StringBuilder sb=new StringBuilder("update business_gType SET ifUse=#{gType.ifUse} where id in (");
        sb.append(forAllGType(gType));
        sb.append(")");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc: 批量删除
     * @Date:  2018/3/9
     * @Param
     */
    public String deleteAll(Map map){
        GType gType=(GType)map.get("gType");
        StringBuilder sb=new StringBuilder("delete from business_gType where id in (");
        sb.append(forAllGType(gType));
        sb.append(")");
        return sb.toString();
    }
    /**
     * @Author: zy
     * @Desc: id用逗号隔开
     * @Date:  2018/3/9
     * @Param
     */
    private String forAllGType( GType gType) {
        String arr[] = gType.getStr().split(",");
        StringBuilder sb = new StringBuilder("'" + arr[0] + "'");
        for (int i = 1; i < arr.length; i++) {
            sb.append(",'" + arr[i] + "' ");
        }
        return sb.toString();
    }
}
